package com.yixq.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.yixq.bean.AssetOrderPool;
import com.yixq.dao.AssetOrderPoolDao;
import com.yixq.service.HttpRequestService;
import com.yixq.vo.FkBackRequestVo;

@Controller
@RequestMapping("/fk")
public class FkBackDDController {

	private static Logger logger = LoggerFactory.getLogger(FkBackDDController.class);

	@Autowired
	private AssetOrderPoolDao assetOrderPoolDao;

	@RequestMapping("backDD")
	public String fkBackDD(Map<String, Object> map) {
		return "fk_backDD";

	}

	@RequestMapping("fk_call_back")
	public String fkCallBack(HttpServletRequest req, HttpServletResponse res,
			Map<String, Object> map) {
		logger.error("******fk_call_back-begin*******");
		FkBackRequestVo requestVo = new FkBackRequestVo();
		Map<String,Object> reqMap=new HashMap<String,Object>();
		String resStr ="调用失败";
		try {
			requestVo.setAssetID(req.getParameter("AssetID"));
			requestVo.setAssetStatus(req.getParameter("AssetStatus"));
			reqMap.put("AssetID", requestVo.getAssetID());
			reqMap.put("AssetStatus", requestVo.getAssetStatus());
			resStr= HttpRequestService.getRequestFkBackDD(requestVo,reqMap);
			if(StringUtils.isEmpty(resStr)){
				map.put("resStr", "无结果返回");
				return "fk_back_result";
			}
			try{
				JSONObject json=JSONObject.parseObject(resStr);
				if(null!=json&&null!=json.getString("result")&&json.getString("result").equals("success")){
					List<AssetOrderPool>  list= assetOrderPoolDao.findByYurref(requestVo.getAssetID());
					logger.error("*****list-size:"+list.size());
					if(null!=list&&list.size()>0){
						AssetOrderPool entity=list.get(0);
						logger.error(JSONObject.toJSONString(entity));
						entity.setStatus(22);
						assetOrderPoolDao.save(entity);
					}
				}
			}catch(JSONException e){
				logger.error("",e);
			}
			
			
		} catch (Exception e) {
			logger.error("",e);
		}
		map.put("resStr", resStr);
		return "fk_back_result";
	}

}
